<?php
/**
 * 文档关键词管理
 *
 * @version   $Id: article_keywords_main.php 1 14:12 2010年7月12日 $
 * @package   DedeCMS.Administrator
 * @founder   IT柏拉图, https://weibo.com/itprato
 * @author    DedeCMS团队
 * @copyright Copyright (c) 2007 - 2021, 上海卓卓网络科技有限公司 (DesDev, Inc.)
 * @license   http://help.dedecms.com/usersguide/license.html
 * @link      http://www.dedecms.com
 */
require_once dirname(__FILE__) . "/config.php";
CheckPurview('sys_Keyword');
require_once DEDEINC . "/datalistcp.class.php";
setcookie("ENV_GOBACK_URL", $dedeNowurl, time() + 3600, "/");
if (empty($dopost)) {
    $dopost = '';
}
$ENV_GOBACK_URL = empty($_COOKIE['ENV_GOBACK_URL']) ? "article_keywords_main.php" : $_COOKIE['ENV_GOBACK_URL'];

//更新
if ($dopost === 'update') {
    if (!isset($aid)) {
        ShowMsg("你没有选择要修改的内容！</br>", $ENV_GOBACK_URL);
        exit();
    }
    $aid = preg_replace("#[^0-9]#", "", $aid);
    $rank = preg_replace("#[^0-9]#", "", $rank);
    $rpurl = trim($rpurl);
    $keyword = trim($keyword);
    $dsql->ExecuteNoneQuery("UPDATE `#@__keywords` SET keyword='$keyword',rpurl='$rpurl',rank='$rank' WHERE aid='$aid';");
    ShowMsg("更新成功！<br />", $ENV_GOBACK_URL);
    exit();
} 
// 删除
else if ($dopost == 'del') {
    if (!isset($aid)) {
        ShowMsg("你没有选择要修改的内容！</br>", $ENV_GOBACK_URL);
        exit();
    }
    //删除项目
    $aid = preg_replace("#[^0-9]#", "", $aid);
    $dsql->ExecuteNoneQuery("DELETE FROM `#@__keywords` WHERE aid='$aid'");
    ShowMsg("删除成功！<br />", $ENV_GOBACK_URL);
    exit();
}
// 禁用项目
else if ($dopost == 'disable') {
    if (!isset($aid)) {
        ShowMsg("你没有选择要修改的内容！</br>", $ENV_GOBACK_URL);
        exit();
    }
    $aid = preg_replace("#[^0-9]#", "", $aid);
    $staold = preg_replace("#[^0-9]#", "", $staold);
    if ($staold == 0) {
        $sta = true;
    }else{
        $sta = false;
    }
    $query = "UPDATE `#@__keywords` SET sta='$sta' WHERE aid='$aid' ";
    $dsql->ExecuteNoneQuery($query);
    if ($sta === true) {
        ShowMsg("关键词已启用！</br>", $ENV_GOBACK_URL);
    }else{
        ShowMsg("关键词已禁用！</br>", $ENV_GOBACK_URL);
    }
    exit();
}
else if ($dopost == 'delall') {
    if (!isset($aids)) {
        ShowMsg("你没有选择要修改的内容！</br>", $ENV_GOBACK_URL);
        exit();
    }
    foreach (explode(",", $aids) as $aid) {
        $aid = preg_replace("#[^0-9]#", "", $aid);
        $dsql->ExecuteNoneQuery("DELETE FROM `#@__keywords` WHERE aid='$aid'");
    }
    ShowMsg("已删除选中内容！</br>", $ENV_GOBACK_URL);
    exit();
}
//增加关键字
else if ($dopost == 'add') {
    $ENV_GOBACK_URL = empty($_COOKIE['ENV_GOBACK_URL']) ? "-1" : $_COOKIE['ENV_GOBACK_URL'];
    $keyword = trim($keyword);
    $rank = preg_replace("#[^0-9]#", '', $rank);
    if ($keyword == '') {
        ShowMsg("关键字不能为空！</br>", -1);
        exit();
    }
    $row = $dsql->GetOne("SELECT * FROM `#@__keywords` WHERE keyword LIKE '$keyword'");
    if (is_array($row)) {
        ShowMsg("关键字已存在库中！</br>", "-1");
        exit();
    }
    $inquery = "INSERT INTO `#@__keywords`(keyword,rank,sta,rpurl) VALUES ('$keyword','$rank','1','$rpurl');";
    $dsql->ExecuteNoneQuery($inquery);
    ShowMsg("成功增加一个关键字！</br>", $ENV_GOBACK_URL);
    exit();
}
if (empty($keyword)) {
    $keyword = '';
    $addquery = '';
} else {
    $addquery = " WHERE keyword LIKE '%$keyword%' ";
}

$sql = "SELECT * FROM `#@__keywords` $addquery ORDER BY rank DESC";
$dlist = new DataListCP();
$dlist->pageSize = 20;
$dlist->SetParameter("keyword", $keyword);
$dlist->SetTemplate(DEDEADMIN . "/templets/article_keywords_main.htm");
$dlist->SetSource($sql);
$dlist->Display();


